package com.ltmonitor.service.impl;

import java.io.Serializable;
import java.util.*;

import com.ltmonitor.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ltmonitor.dao.IVideoFileItemDao;
import com.ltmonitor.video.entity.VideoFileItem;
import com.ltmonitor.service.IVideoFileItemService;

@Service("videoFileItemService")
public class VideoFileItemService extends BaseService<Long, VideoFileItem> implements IVideoFileItemService {

	@Autowired
	private IVideoFileItemDao VideoFileItemDao;

	/**
	 * 根据下发的检索指令Id，获取终端返回的视频文件列表
	 *
	 * @param commandId 用户下发指令后，数据库自动生成的命令记录的主键ID
	 * @return 、
	 */
	public List<VideoFileItem> getVideoFileItemListByCommandId(long commandId) {
		String hsql = "from VideoFileItem where commandId = ?";
		return this.query(hsql,
				commandId);
	}

	public VideoFileItem getFtpVideoFileItemByCommandId(long commandId) {
		String hsql = "from VideoFileItem where commandId = ?";
		return this.find(hsql,
				commandId);
	}


	public VideoFileItem updateVideoFileItemStatusCommandId(long commandId, String status) {
		String hsql = "from VideoFileItem where commandId = ?";
		VideoFileItem v = this.find(hsql,
				commandId);
		if (v != null) {
			v.setStatus(status);
			v.setUploadDate(new Date());
			this.saveOrUpdate(v);
		}
		return v;
	}

	/**
	 * 停止录像
	 * @param simNo
	 * @param channel
	 */
	public void endRecordFile(String simNo, int channel)
	{
		String hql = "update VideoFileItem set status = ? where createDate > ? and (status = ? or status = ? )  and fileSource = ? and simNo = ? and channelId = ?";
		Date d = DateUtil.getDate(new Date(), Calendar.HOUR,-1);
		this.bulkUpdate(hql, new Object[]{ VideoFileItem.STATUS_STOP_RECORD,d,  VideoFileItem.STATUS_NOT_UPLOAD,
				VideoFileItem.STATUS_UPLOADING, VideoFileItem.FROM_USER,simNo, (byte)channel});
	}

	public VideoFileItem getRecordFile(String simNo, int channel)
	{
		String hsql = "from VideoFileItem where createDate > ? and status = ? and fileSource = ?  and simNo = ? and channelId = ? order by createDate desc";
		Date d = DateUtil.getDate(new Date(), Calendar.HOUR,-1);
		VideoFileItem v = this.find(hsql,
				new Object[]{d, VideoFileItem.STATUS_UPLOAD_COMPLTETED,  VideoFileItem.FROM_USER,simNo, (byte)channel });
		return v;
	}







}
